﻿#region zh-CHS 2006 - 2007 DemoSoft 团队 | en 2006 - 2007 DemoSoft Team

//     NOTES
// ---------------
//
// This file is a part of the G.O.S.E(Game Online Server Engine) for .NET.
//
//                 2006-2007 DemoSoft Team
//
//
// First Version : by H.Q.Cai - mailto:caihuanqing@hotmail.com

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

#region zh-CHS 包含名字空间 | en Include namespace
using System;
using System.Text;
using System.Linq;
using System.Threading;
using System.Diagnostics;
using System.Collections.Generic;
using Demo_G.O.S.E.ServerEngine.Util;
using Demo_G.O.S.E.ServerEngine.Common;
#endregion

namespace Demo_G.O.S.E.ServerEngine.Item
{
    public class BaseItemContainer : BaseItem
    {
        #region zh-CHS 构造和初始化和清理 | en Constructors and Initializers and Dispose
        /// <summary>
        /// 
        /// </summary>
        public BaseItemContainer( long iMaxSlot )
        {
            m_MaxSlot = iMaxSlot;
        }

        /// <summary>
        /// 
        /// </summary>
        public BaseItemContainer( long iMinSlot, long iMaxSlot )
        {
            m_MinSlot = iMinSlot;
            m_MaxSlot = iMaxSlot;
        }

        /// <summary>
        /// 
        /// </summary>
        public BaseItemContainer( long iMinSlot, long iMaxSlot, Serial serial )
            : base( serial )
        {
            m_MinSlot = iMinSlot;
            m_MaxSlot = iMaxSlot;
        }
        #endregion

        #region zh-CHS 共有属性 | en Public Properties

        #region zh-CHS MinSlot属性 | en Public Properties
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private long m_MinSlot = 0;
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public long MinSlot
        {
            get { return m_MinSlot; }
        }

        #region zh-CHS 保护方法 | en Protected Methods

        /// <summary>
        /// 
        /// </summary>
        /// <param name="iMinSlot"></param>
        protected void SetMinSlot( long iMinSlot )
        {
            m_MinSlot = iMinSlot;
        }

        #endregion

        #endregion

        #region zh-CHS MaxSlot属性 | en Public Properties
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private long m_MaxSlot = long.MaxValue;
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public long MaxSlot
        {
            get { return m_MaxSlot; }
        }

        #region zh-CHS 保护方法 | en Protected Methods

        /// <summary>
        /// 
        /// </summary>
        /// <param name="iMinSlot"></param>
        protected void SetMaxSlot( long iMaxSlot )
        {
            m_MaxSlot = iMaxSlot;
        }

        #endregion

        #endregion

        #region zh-CHS IsContainerFull属性 | en Public Properties
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public bool IsContainerFull
        {
            get
            {
                if ( m_ToteSubItems.Count < ( ( this.MaxSlot - this.MinSlot ) + 1 ) )
                    return false;
                else
                    return true;
            }
        }
        #endregion

        #region zh-CHS BaseItemContainerState属性 | en Public Properties

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 道具的的状态
        /// </summary>
        private BaseItemContainerState m_BaseItemContainerState = new BaseItemContainerState();
        #endregion
        /// <summary>
        /// 道具的的状态
        /// </summary>
        public BaseItemContainerState BaseItemContainerState
        {
            get { return m_BaseItemContainerState; }
        }

        #endregion

        #region zh-CHS 道具列表 属性 | en Public Properties

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 人物的道具列表
        /// </summary>
        private SafeDictionary<long, BaseItem> m_ToteSubItems = new SafeDictionary<long, BaseItem>();
        #endregion
        /// <summary>
        /// 人物的道具列表(包裹与身上)
        /// </summary>
        /// <returns></returns>
        [MultiThreadedWarningAttribute( "(道具)当前的数组是列表临时产生的,不能保存数组用于以后操作:警告!" )]
        public BaseItem[] SubItemsToArray()
        {
            return m_ToteSubItems.ToArray();
        }

        #region zh-CHS 共有方法 | en Public Methods

        /// <summary>
        /// 添加道具
        /// </summary>
        /// <param name="item"></param>
        public bool AddSubItem( long iItemSlot, BaseItem baseItem )
        {
            if ( baseItem == null )
            {
                Debug.WriteLine( "BaseItemContainer.AddSubItem(...) - baseItem == null error!" );

                return false;
            }

            if ( base.Deleted == true )
                return false;

            if ( iItemSlot > this.MaxSlot || iItemSlot < this.MinSlot )
                return false;

            BaseItem findBaseItem = FindSubItemAtSlot( iItemSlot );
            if ( findBaseItem != null )
            {
                Debug.WriteLine( "BaseItemContainer.AddSubItem(...) - findBaseItem != null error!" );

                return false;
            }

            EventHandler<BeforeAddSubItemCallEventArgs> tempBeforeEventArgs = m_ThreadEventBeforeAddSubItemCall;
            if ( tempBeforeEventArgs != null )
            {
                BeforeAddSubItemCallEventArgs eventArgs = new BeforeAddSubItemCallEventArgs( baseItem, this );
                tempBeforeEventArgs( this, eventArgs );

                if ( eventArgs.IsCancel == true )
                    return false;
            }

            m_ToteSubItems.Add( iItemSlot, baseItem );
            baseItem.SetSlotId( iItemSlot );

            m_BaseItemContainerState.UpdateAddSubItemCall();

            EventHandler<AfterAddSubItemCallEventArgs> tempAfterEventArgs = m_ThreadEventAfterAddSubItemCall;
            if ( tempAfterEventArgs != null )
            {
                AfterAddSubItemCallEventArgs eventArgs = new AfterAddSubItemCallEventArgs( baseItem, this );
                tempAfterEventArgs( this, eventArgs );
            }

            return true;
        }

        /// <summary>
        /// 移出道具
        /// </summary>
        /// <param name="item"></param>
        public bool RemoveSubItem( long iItemSlot )
        {
            if ( base.Deleted == true )
                return false;

            if ( iItemSlot > this.MaxSlot || iItemSlot < this.MinSlot )
                return false;

            BaseItem baseItem = FindSubItemAtSlot( iItemSlot );
            if ( baseItem == null )
                return false;

            EventHandler<BeforeRemoveSubItemCallEventArgs> tempBeforeEventArgs = m_ThreadEventBeforeRemoveSubItemCall;
            if ( tempBeforeEventArgs != null )
            {
                BeforeRemoveSubItemCallEventArgs eventArgs = new BeforeRemoveSubItemCallEventArgs( baseItem, this );
                tempBeforeEventArgs( this, eventArgs );

                if ( eventArgs.IsCancel == true )
                    return false;
            }

            m_ToteSubItems.Remove( iItemSlot );
            baseItem.SetSlotId( -1 );

            m_BaseItemContainerState.UpdateRemoveSubItemCall();

            EventHandler<AfterRemoveSubItemCallEventArgs> tempAfterEventArgs = m_ThreadEventAfterRemoveSubItemCall;
            if ( tempAfterEventArgs != null )
            {
                AfterRemoveSubItemCallEventArgs eventArgs = new AfterRemoveSubItemCallEventArgs( baseItem, this );
                tempAfterEventArgs( this, eventArgs );
            }

            return true;
        }

        /// <summary>
        /// 找道具
        /// </summary>
        /// <param name="layer"></param>
        /// <returns></returns>
        public BaseItem FindSubItemAtSlot( long iItemSlot )
        {
            return m_ToteSubItems.GetValue( iItemSlot );
        }

        /// <summary>
        /// 
        /// </summary>
        public void EmptyContainer()
        {
            m_ToteSubItems.Clear();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        public bool AddToFreeSlot( BaseItem baseItem )
        {
            long iRetrunItemSlot = -1;
            if ( this.FindFreeSlot( ref iRetrunItemSlot ) == false )
                return false;

            if ( this.AddSubItem( iRetrunItemSlot, baseItem )  == false )
                return false;

            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="layer"></param>
        /// <returns></returns>
        public bool FindFreeSlot( ref long iRetrunItemSlot )
        {
            iRetrunItemSlot = -1;

            if ( base.Deleted == true )
                return false;

            if ( this.IsContainerFull == true )
                return false;

            for ( long iIndexSlot = this.MinSlot; iIndexSlot <= this.MaxSlot; iIndexSlot++ )
			{
                BaseItem baseItem = FindSubItemAtSlot( iIndexSlot );

                if ( baseItem == null )
                {
                    iRetrunItemSlot = iIndexSlot;
                    return true;
                }
			}

            return false;
        }
        #endregion

        #endregion

        #endregion

        #region zh-CHS 共有事件 | en Public Event

        #region zh-CHS AddSubItemCall事件 | en Public Event
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private EventHandler<BeforeAddSubItemCallEventArgs> m_ThreadEventBeforeAddSubItemCall;
        /// <summary>
        /// 
        /// </summary>
        private ReaderWriterLockSlim m_LockThreadEventBeforeAddSubItemCall = new ReaderWriterLockSlim();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public event EventHandler<BeforeAddSubItemCallEventArgs> ThreadBeforeAddSubItemCall
        {
            add
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventBeforeAddSubItemCall );
                {
                    m_ThreadEventBeforeAddSubItemCall += value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventBeforeAddSubItemCall );
            }
            remove
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventBeforeAddSubItemCall );
                {
                    m_ThreadEventBeforeAddSubItemCall -= value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventBeforeAddSubItemCall );
            }
        }

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private EventHandler<AfterAddSubItemCallEventArgs> m_ThreadEventAfterAddSubItemCall;
        /// <summary>
        /// 
        /// </summary>
        private ReaderWriterLockSlim m_LockThreadEventAfterAddSubItemCall = new ReaderWriterLockSlim();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public event EventHandler<AfterAddSubItemCallEventArgs> ThreadAfterAddSubItemCall
        {
            add
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventAfterAddSubItemCall );
                {
                    m_ThreadEventAfterAddSubItemCall += value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventAfterAddSubItemCall );
            }
            remove
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventAfterAddSubItemCall );
                {
                    m_ThreadEventAfterAddSubItemCall -= value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventAfterAddSubItemCall );
            }
        }
        #endregion

        #region zh-CHS RemoveSubItemCall事件 | en Public Event
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private EventHandler<BeforeRemoveSubItemCallEventArgs> m_ThreadEventBeforeRemoveSubItemCall;
        /// <summary>
        /// 
        /// </summary>
        private ReaderWriterLockSlim m_LockThreadEventBeforeRemoveSubItemCall = new ReaderWriterLockSlim();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public event EventHandler<BeforeRemoveSubItemCallEventArgs> ThreadBeforeRemoveSubItemCall
        {
            add
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventBeforeRemoveSubItemCall );
                {
                    m_ThreadEventBeforeRemoveSubItemCall += value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventBeforeRemoveSubItemCall );
            }
            remove
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventBeforeRemoveSubItemCall );
                {
                    m_ThreadEventBeforeRemoveSubItemCall -= value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventBeforeRemoveSubItemCall );
            }
        }

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private EventHandler<AfterRemoveSubItemCallEventArgs> m_ThreadEventAfterRemoveSubItemCall;
        /// <summary>
        /// 
        /// </summary>
        private ReaderWriterLockSlim m_LockThreadEventAfterRemoveSubItemCall = new ReaderWriterLockSlim();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public event EventHandler<AfterRemoveSubItemCallEventArgs> ThreadAfterRemoveSubItemCall
        {
            add
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventAfterRemoveSubItemCall );
                {
                    m_ThreadEventAfterRemoveSubItemCall += value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventAfterRemoveSubItemCall );
            }
            remove
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventAfterRemoveSubItemCall );
                {
                    m_ThreadEventAfterRemoveSubItemCall -= value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventAfterRemoveSubItemCall );
            }
        }
        #endregion

        #endregion
    }
}
#endregion